<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="" prop="keyWord">
        <el-input placeholder="请输入内容" v-model.trim="keyWord">
          <template #prepend>
            <el-select v-model="kType" placeholder="请选择" class="!w-120px">
              <el-option label="楼栋名称" value="houseName" />
              <el-option label="房间名称" value="roomName" />
              <el-option label="房间地址编码" value="roomAddrCode" />
              <el-option label="通通锁设备MAC" value="ttlDeviceMac" />
              <el-option label="通通锁内置名称" value="ttlDeviceName" />
            </el-select>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="授权状态" prop="ttlHouseAccountAuthFlag">
        <el-select
          v-model="queryParams.ttlHouseAccountAuthFlag"
          placeholder="请选择授权状态"
          clearable
          class="!w-240px"
        >
        <el-option
v-for="dict in getIntDictOptions(DICT_TYPE.TTL_HOUSE_AUTH_STATUS)"
                    :key="parseInt(dict.value)"
                    :label="dict.label"
                    :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="地址" align="left" width="220">
        <template #default="scope">
          <div>楼栋：{{scope.row.houseName}}</div>
          <div>房间：{{scope.row.roomName}}</div>
          <div>分组ID：{{scope.row.ttlGroupId}}</div>
        </template>
      </el-table-column>
      <el-table-column label="接口调用" align="left" width="180">
        <template #default="scope">
          <div>楼栋用户授权：
            <dict-tag :type="DICT_TYPE.TTL_HOUSE_AUTH_STATUS" :value="scope.row.ttlHouseAccountAuthFlag" />
          </div>
          <div>锁重置状态：
            <dict-tag :type="DICT_TYPE.TTL_LOCK_RESET_STATUS" :value="scope.row.deleteStepFlag" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="通通锁" align="center">
        <el-table-column label="设备" align="left" prop="ttlDeviceMac" width="190">
          <template #default="scope">
            <div>锁ID：{{scope.row.ttlLockId}}</div>
            <div>MAC：{{scope.row.ttlDeviceMac}}</div>
            <div>内置名称：{{scope.row.ttlDeviceName}}</div>
          </template>
        </el-table-column>
        <el-table-column label="蓝牙钥匙ID" align="left" prop="ttlDeviceMac" width="190">
          <template #default="scope">
            <div>楼栋：{{scope.row.authKeyId}}</div>
            <div>社区：{{scope.row.ttlKeyId}}</div>
          </template>
        </el-table-column>
        <el-table-column label="账号ID" align="left" prop="ttlDeviceMac" width="110">
          <template #default="scope">
            <div>楼栋：{{scope.row.ttlHouseAccountId}}</div>
            <div>社区：{{scope.row.ttlVillageAccountId}}</div>
          </template>
        </el-table-column>
        <el-table-column label="锁特征值" align="left" prop="ttlFeatureValue" width="150"/>
        <el-table-column label="支持功能" align="left" prop="ttlDeviceMac" width="120">
          <template #default="scope">
            <div>远程开门：{{scope.row.supportRemoteFlag == 1 ? "是":""}}</div>
            <div>指纹开门：{{scope.row.supportFingerFlag == 1 ? "是":""}}</div>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column
        label="楼栋管理员授权成功时间"
        align="center"
        prop="authSuccessTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <TtlLockForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { TtlLockApi, TtlLockVO } from '@/api/zwsj/ttllock'

/** 门锁管理 列表 */
defineOptions({ name: 'TtlLock' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<TtlLockVO[]>([]) // 列表的数据
// 列表的总页数
const total = ref(0)
const keyWord = ref('')
const kType = ref('houseName')
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  commId: undefined,
  houseName: '',
  roomName: '',
  roomAddrCode: '',
  ttlDeviceMac: '',
  ttlDeviceName: '',
})
const queryFormRef = ref() // 搜索的表单

const initKeyWord = async () => {
  queryParams.houseName= ''
  queryParams.roomName= ''
  queryParams.roomAddrCode= ''
  queryParams.ttlDeviceMac= ''
  queryParams.ttlDeviceName= ''
  if (kType.value == 'houseName') {
    queryParams.houseName = keyWord.value
  }else if (kType.value == 'roomName') {
    queryParams.roomName = keyWord.value
  }else if (kType.value == 'roomAddrCode') {
    queryParams.roomAddrCode = keyWord.value
  }else if (kType.value == 'ttlDeviceMac') {
    queryParams.ttlDeviceMac = keyWord.value
  }else if (kType.value == 'ttlDeviceName') {
    queryParams.ttlDeviceName = keyWord.value
  }
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    initKeyWord()
    const data = await TtlLockApi.getTtlLockPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>